<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *输出使用
 *
 * @author eoe2005
 */
class Out {
    /**
     *输出错误信息
     * @param type $code
     */
    public static  function showError($code){
        $obj = array('error' => $code);
        self::show($obj);
    }
    /**
     *享用户输出内容
     * @param type $obj
     */
    public static function show($obj){
        if(get('_t_') == 'xml'){
            self::outXML($obj);
        }elseif(get('_t_') == 'jsonp'){
            $call = get('_callback_');
            self::outJSONP($obj,$call);
        }else{
            self::outJSON($obj);
        }
        die();
    }
    /**
     *返回json格式的数据
     * @param type $Obj
     */
    private static function outJSON($Obj){
        echo json_encode($Obj);
    }
    /**
     *返回xml格式的数据
     * @global type $control
     * @global type $action
     * @param type $Obj
     */
    private static function outXML($Obj){
        include __ROOT.'libs/smarty3/Smarty.class.php';
        $smarty = new Smarty;
        $smarty->setTemplateDir( APP_TEMPLATES);
        $smarty->setCompileDir(__ROOT.'smarty/cache');
        $smarty->setPluginsDir(__ROOT.'smarty/plugins');
        global $control,$action;
        $smarty->assign('rs',$Obj);
        $smarty->display(sprintf("%s/%s.xml",strtolower($control),$action));
    }
    /**
     *回返JSONP格式的数据
     * @param type $Obj
     * @param string $callFunction
     */
    private static function outJSONP($Obj,$callFunction = 'callback'){
        if(empty($callFunction)){
            $callFunction = 'callback';
        }
        echo sprintf('%s(%s);',$callFunction,json_encode($Obj));
    }
}

?>
